package com.bookmanger.controller;

import cn.hutool.core.bean.BeanUtil;
import com.bookmanger.common.result.Result;
import com.bookmanger.common.utils.UserIdThreadLocal;
import com.bookmanger.entity.Order;
import com.bookmanger.entity.dto.BookBorrowDTO;
import com.bookmanger.entity.dto.OrderDTO;
import com.bookmanger.entity.dto.OrderRankDTO;
import com.bookmanger.entity.request.BookBorrowRequest;
import com.bookmanger.entity.request.BookSearchRequest;
import com.bookmanger.entity.select.OrderBookListDTO;
import com.bookmanger.entity.vo.BookCategoryCountVO;
import com.bookmanger.entity.vo.ListForm;
import com.bookmanger.entity.vo.OrderBookListVo;
import com.bookmanger.service.BookBorrowService;
import com.bookmanger.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangfuhao
 * @folder 图书借阅
 * @Date 2024/11/2
 */
@RestController
@RequestMapping("/api/bookBorrow")
public class BookBorrowController {

    @Resource
    private BookBorrowService bookBorrowService;

    @Resource
    private OrderService orderService;

//    public BookBorrowController(BookBorrowService bookBorrowService) {
//        this.bookBorrowService = bookBorrowService;
//    }

    /**
     * 借阅图书
     *
     * @param bookBorrowRequest
     * @return
     */
    @PostMapping("/borrow")
    public Result<BookBorrowDTO> borrow(@RequestBody BookBorrowRequest bookBorrowRequest) {
        return bookBorrowService.borrow(BeanUtil.copyProperties(bookBorrowRequest, Order.class));
    }

    /**
     * 还书
     *
     * @param bookBorrowRequest
     * @return
     */
    @PostMapping("/restore")
    public Result<BookBorrowDTO> restore(@RequestBody BookBorrowRequest bookBorrowRequest) {
        return bookBorrowService.restore(BeanUtil.copyProperties(bookBorrowRequest, Order.class));
    }

    /**
     * 借阅记录查询
     *
     * @return
     */
    @GetMapping("record")
    public Result<List<BookBorrowDTO>> record() {
        return bookBorrowService.record();
    }

    /**
     * 管理端获取用户借阅列表
     *
     * @param orderBookListDTO
     * @return
     */
    @PostMapping("/OrderList")
    public Result<ListForm<OrderBookListVo>> getOrderList(@RequestBody OrderBookListDTO orderBookListDTO) {
        return bookBorrowService.getOrderList(orderBookListDTO);
    }

    /**
     * 借阅排行榜
     *
     * @return
     */
    @GetMapping("/rank")
    public Result<List<OrderRankDTO>> getOrderRank() {
        return orderService.getOrderRank();
    }

    /**
     * 获取借阅总数
     *
     * @return
     */
    @GetMapping("/getOrderCount")
    public Result<Integer> getOrderCount() {
        return orderService.getOrderCount();
    }

    /**
     * 未处理的借阅单数量
     *
     * @return
     */
    @GetMapping("/getOrderCountNullHandle")
    public Result<Integer> getOrderCountNullHandle() {
        return orderService.getOrderCountNullHandle();
    }

    /**
     * 获取借阅书籍的分类数量
     *
     * @return
     */
    @GetMapping("/getOrderBookCategoryCount")
    public Result<List<BookCategoryCountVO>> getOrderBookCategoryCount() {
        return orderService.getOrderBookCategoryCount();
    }

    /**
     * 管理员通过借阅预约
     */
    @PostMapping("/passBorrow")
    public Result<BookBorrowDTO> passBorrow(@RequestBody BookBorrowRequest bookBorrowRequest) {
        return bookBorrowService.passBorrow(BeanUtil.copyProperties(bookBorrowRequest, Order.class));
    }

    /**
     * 管理员确认归还
     */
    @PostMapping("/passRestore")
    public Result<BookBorrowDTO> passRestore(@RequestBody BookBorrowRequest bookBorrowRequest) {
        return bookBorrowService.passRestore(BeanUtil.copyProperties(bookBorrowRequest, Order.class));
    }

    /**
     * 管理员拒绝归还
     */
    @PostMapping
    public Result<BookBorrowDTO> refuseRestore(@RequestBody BookBorrowRequest bookBorrowRequest) {
        return bookBorrowService.refuseRestore(BeanUtil.copyProperties(bookBorrowRequest, Order.class));
    }

    /**
     * 管理员查看该图书借阅记录
     */
    @GetMapping("/orders")
    public Result<List<OrderDTO>> orders(BookBorrowRequest bookBorrowRequest) {
        return bookBorrowService.orders(BeanUtil.copyProperties(bookBorrowRequest, Order.class));
    }
}
